package com.mycompany.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;  
import org.primefaces.model.TreeNode;
/**
 *
 * @author Muharrem Tigdemir
 */
@ManagedBean
@ViewScoped
public class MainController extends Connector implements Serializable{
    
    private static final long serialVersionUID = 7863262235394607247L;
    private ArrayList<DatabaseBean> databases;
    private ArrayList<User> users;
    private TreeNode root;  
    private TreeNode selectedNode; 
    private TableBean selectedTable;
    
    public MainController() {
        databases = getDatabaseMap();
        this.init();
        generateUI();
    }
    
    @PostConstruct
    private void sayHi(){
        //FacesContext context = FacesContext.getCurrentInstance(); 
        //context.addMessage(null, new FacesMessage("Successful", "Hello "));  
    }
    
    private void generateUI()
    {
        root = new DefaultTreeNode("Root", null);  
        
        for (DatabaseBean databaseBean : databases) {
            DefaultTreeNode dbNode = new DefaultTreeNode("databaseNode" , databaseBean.getName(), root);
            for (CollectionBean collectionBean : databaseBean.getCollections()) {
                DefaultTreeNode collectionNode = new DefaultTreeNode("collectionNode" , collectionBean.getName(), dbNode);
            }
        }
    }

    /*METHODS*/
    
     public void onNodeSelect(NodeSelectEvent event) {    
    }  
    
     public void displayCollection()
     {
          if(selectedNode != null) {  
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Collection Listed", selectedNode.getData().toString()); 
            FacesContext.getCurrentInstance().addMessage(null, message);
        }  
     }
     
     
     //Drop Database
     public void dropDatabase()
     {
         setCurrentDBClient();
         System.out.println("Dropped");
         //database.dropDatabase();
         
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "System Error",  "Please try again later.");  
          
        FacesContext.getCurrentInstance().addMessage(null, message);  
     }
     
     //Show Statisctics
    
     public void showDatabaseStatistics()
     {
         setCurrentDBClient();
         System.out.println(database.getStats().toString());
     }
     
     public void setCurrentDBClient()
     {
          database  = client.getDB(selectedNode.getData().toString());
     }
    
    /*GETTER SETTER*/
    public TableBean getSelectedTable() {
        return selectedTable;
    }

    public void setSelectedTable(TableBean selectedTable) {
        this.selectedTable = selectedTable;
    }
     
    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }
    
    public ArrayList<DatabaseBean> getDatabases() {
        return databases;
    }

    public void setDatabases(ArrayList<DatabaseBean> databases) {
        this.databases = databases;
    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<User> users) {
        this.users = users;
    }
}
